﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SphereOnWave : MonoBehaviour
{
    private float CaculatePos(float A, float w, float fi, float t, Vector2 direction, Vector3 p){
        direction.Normalize();
        Vector2 pos = new Vector2(p.x, p.z);
        float H = A * Mathf.Sin(Vector2.Dot(pos, direction) * w + t * fi);
        return H;
    }

    private float A = 1;
    private float fi = 2;
    private float w = 0.15f;

    // Update is called once per frame
    void Update()
    {
        float H = 0;
        Vector3 worldPos = transform.position;
        //t是自该场景加载开始所经过的时间，4个分量分别是 (t/20, t, t*2, t*3)_Time float4  time(t / 20, t, t * 2, t * 3),
        //因为shader里取的是x值，因此这里除20保持两边一致
        float time = Time.time / 20;
        H += CaculatePos(A / 8, w, fi, time * 80, new Vector2(-1, 1), worldPos);
        H += CaculatePos(A, w, fi, time * 60, new Vector2(2, -0.5f), worldPos);
        H += CaculatePos(A, w, fi, time * 20, new Vector2(1, -2), worldPos);
        H += CaculatePos(A * 2, w, fi, time * 40, new Vector2(1, -1), worldPos);

        transform.position = new Vector3(worldPos.x, H, worldPos.z);
    }
}
